package com.king01.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.king01.dao.OutlibraryDao;
import com.king01.entity.Branch_goods;
import com.king01.entity.Outlibrary_goods;
import com.king01.entity.Outlibrarytransfer_goods;
import com.king01.entity.Staff;
import com.king01.service.OutlibraryService;
import com.king01.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author ：lqs
 * @date ：Created in 2023/10/2 11:08
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class OutlibraryServiceImpl implements OutlibraryService {
    @Autowired
    private OutlibraryDao outlibraryDao;
    @Autowired
    private HttpSession session;

    @Override
    public Result selectAll(Integer currentPage, Integer pageSize) {
        //mybatis-pageHelper插件
        PageHelper.startPage(currentPage, pageSize);
        Staff staff = (Staff) session.getAttribute("staff");
        List<OutlibraryVo> outlibrarytransferVos = outlibraryDao.selectAll(staff.getBranchid());
        PageInfo<OutlibraryVo> pageInfo = new PageInfo<>(outlibrarytransferVos);
        return new Result(200, "出库审核数据", pageInfo);
    }

    //添加出库审核的数据
    @Override
    public Result addOutlibrary(Integer outmethod) {
        Date date = new Date();
        Staff staff = (Staff) session.getAttribute("staff");
        Integer integer = outlibraryDao.addOutlibrary(staff, date, outmethod);
        return new Result(200, "出库审核数据添加成功", integer);
    }

    @Override
    public Result lsaddOutlibrary(Integer outmethod) {
        Date date = new Date();
        Staff staff = (Staff) session.getAttribute("staff");
        Integer integer = outlibraryDao.lsaddOutlibrary(staff, date, outmethod);
        return new Result(200, "出库审核数据添加成功", integer);
    }

    // 获取outlibrary中最大的id
    @Override
    public Result maxId() {
        Integer integer = outlibraryDao.maxId();
        return new Result(200, "获取outlibrary中最大的id", integer);
    }

    @Override
    public Result addOutlibraryGoods(OutlibraryVo2 outlibraryVo2, Integer cid) {
        Integer sprice = outlibraryVo2.getSprice();
        Integer num = outlibraryVo2.getNum();
        Integer total = sprice * num;
        Integer integer = outlibraryDao.addOutlibraryGoods(outlibraryVo2, cid, total);
        return new Result(200, "插入数据成功", integer);
    }

    @Override
    public Result addOutlibraryGoodspl(List<OutlibraryVo2> list, Integer cid) {
        Integer integer = outlibraryDao.addOutlibraryGoodspl(list, cid);
        return new Result(200, "批量出售成功", integer);
    }

    //根据cid查询出出库的商品
    @Override
    public Result selectOutByCid(Integer cid) {
        List<Outlibrary_goods> outlibrary_goods = outlibraryDao.selectOutByCid(cid);
        return new Result(200, "", outlibrary_goods);
    }

    //获取当前店铺库存表的数据
    @Override
    public Result selectBgByBranchid(Integer branchid) {
        Staff staff = (Staff) session.getAttribute("staff");
        List<Branch_goods> branch_goods = outlibraryDao.selectBgByBranchid(staff.getBranchid());
        return new Result(200, "", branch_goods);

    }

    @Override
    public Result selectBgByBranchid2(Integer branchid) {
        List<Branch_goods> branch_goods = outlibraryDao.selectBgByBranchid(branchid);
        return new Result(200, "", branch_goods);
    }

    //修改通过后的数据
    @Override
    public Result updateSuccess(Branch_goods branch_goods) {
        Integer integer = outlibraryDao.updateSuccess(branch_goods);
        return new Result(200, "", integer);

    }

    @Override
    public Result updateoutlibrary(Integer cid) {
        Date date = new Date();
        Staff staff = (Staff) session.getAttribute("staff");
        Integer updateoutlibrary = outlibraryDao.updateoutlibrary(date, cid, staff.getStaffid());
        return new Result(200, "", updateoutlibrary);

    }
    @Override
    public Result updateoutlibrary3(Integer cid) {
        Date date = new Date();
        Staff staff = (Staff) session.getAttribute("staff");
        Integer updateoutlibrary = outlibraryDao.updateoutlibrary3(date, cid, staff.getStaffid());
        return new Result(200, "", updateoutlibrary);

    }

    @Override
    public Result updateoutlibrary4(Integer cid, String remarks) {
        Date date = new Date();
        Staff staff = (Staff) session.getAttribute("staff");
        Integer updateoutlibrary = outlibraryDao.updateoutlibrary4(date, cid, remarks, staff.getStaffid());
        if (updateoutlibrary > 0) {
            return new Result(200, "驳回成功", null);
        } else {
            return new Result(500, "驳回失败", null);
        }
    }

    @Override
    public Result updateBranch_goods(OutlibraryVo2 outlibraryVo2) {
        Staff staff = (Staff) session.getAttribute("staff");
        outlibraryVo2.setBranchid(staff.getBranchid());
        //查询对应的count值
        List<Branch_goods> branch_goods = outlibraryDao.selectBranch_goods(outlibraryVo2);
        outlibraryVo2.setNum(branch_goods.get(0).getCounts()-outlibraryVo2.getNum());
        Integer integer = outlibraryDao.updateBranch_goods(outlibraryVo2);
        return new Result(200, "零售成功", integer);
    }

    @Override
    public Result selectBranch_goods(OutlibraryVo2 outlibraryVo2) {
        return null;
    }

    @Override
    public Result addDoodsByBranchid(Integer branchid, Integer goodsid,Integer counts) {
        Integer integer = outlibraryDao.addDoodsByBranchid(branchid,goodsid,counts);
        return new Result(200, "", integer);
    }

    //驳回时修改outlibrary表的状态和时间
    @Override
    public Result updateoutlibrary2(Integer cid, String remarks) {
        Date date = new Date();
        Staff staff = (Staff) session.getAttribute("staff");
        Integer updateoutlibrary = outlibraryDao.updateoutlibrary2(date, cid, remarks, staff.getStaffid());
        if (updateoutlibrary > 0) {
            return new Result(200, "驳回成功", null);
        } else {
            return new Result(500, "驳回失败", null);
        }

    }

    @Override
    public Result find(Integer cid) {
        List<OutlibraryVo2> outlibraryVo2s = outlibraryDao.find(cid);
        return new Result(200, "查询成功", outlibraryVo2s);
    }

    @Override
    public Result selectAll2(Integer currentPage, Integer pageSize,OutlibraryVo2 outlibraryVo2) {
        Staff staff = (Staff) session.getAttribute("staff");
        if (staff.getStaffid()!=111){
            outlibraryVo2.setBranchid(staff.getBranchid());
        }
        //mybatis-pageHelper插件
        PageHelper.startPage(currentPage, pageSize);
        List<OutlibraryVo2> outlibraryVo2s = outlibraryDao.selectAll2(outlibraryVo2.getBranchid());
        System.out.println("11111"+outlibraryVo2.getBranchid());
        PageInfo<OutlibraryVo2> pageInfo = new PageInfo<>(outlibraryVo2s);
        return new Result(200, "查询成功", pageInfo);
    }
    //平调信息主表数据
    @Override
    public Result selectMain(Integer currentPage, Integer pageSize, Integer branchid) {
        Staff staff = (Staff) session.getAttribute("staff");
        if (staff.getStaffid()!=111){
            branchid = staff.getBranchid();
        }
        //mybatis-pageHelper插件
        PageHelper.startPage(currentPage, pageSize);
        List<StatisticsVo> outlibraryVo2s = outlibraryDao.selectMain(branchid);
        PageInfo<StatisticsVo> pageInfo = new PageInfo<>(outlibraryVo2s);
        return new Result(200, "查询成功", pageInfo);
    }

    @Override
    public Result selectAll3(Integer currentPage, Integer pageSize) {
        //mybatis-pageHelper插件
        PageHelper.startPage(currentPage, pageSize);
        Staff staff = (Staff) session.getAttribute("staff");
        List<TransferExMainVo> transferExMainVos = outlibraryDao.selectAll3(staff.getBranchid());
        PageInfo<TransferExMainVo> pageInfo = new PageInfo<>(transferExMainVos);
        return new Result(200, "数据", pageInfo);
    }

    //平调审核详细信息
    @Override
    public Result find2(Integer cid) {
        List<OutlibraryVo2> outlibraryDao2 = outlibraryDao.find2(cid);
        System.out.println(",,,,"+outlibraryDao2);
        return new Result(200, "数据", outlibraryDao2);
    }

    //平调出库按钮 添加数据
    @Override
    public Result add2(Integer newbranchid) {
        Date date = new Date();
        Staff staff = (Staff) session.getAttribute("staff");
        Integer integer = outlibraryDao.add2(staff.getStaffid(), date, staff.getBranchid(), newbranchid);
        return new Result(200, "平调出库成功", integer);
    }

    @Override
    public Result maxId2() {
        Integer integer = outlibraryDao.maxId2();
        return new Result(200, "数据", integer);
    }

    @Override
    public Result addByOg(Outlibrary_goods outlibrary_goods) {
        Integer integer = outlibraryDao.addByOg(outlibrary_goods);
        return new Result(200, "数据", integer);
    }

    //根据cid查询出出库的商品
    @Override
    public Result selectOutByCid2(Integer cid) {
        List<Outlibrarytransfer_goods> outlibrarytransfer_goods = outlibraryDao.selectOutByCid2(cid);
        return new Result(200, "", outlibrarytransfer_goods);
    }
}

